//
//  CPNewModal.m
//  News
//
//  Created by chen on 15/3/24.
//  Copyright (c) 2015年 chen. All rights reserved.
//

#import "CPNewModal.h"

#import  "CPNetwork.h"
@implementation CPNewModal

+ (void)loadModal:(NSString *)urlString  completion:(CompletionModal)completion{
    CPNetwork *net = [[CPNetwork alloc] init];
    [net requestJSON:urlString httpMethod:HTTPMethodGet parameter:nil completionHandler:^( NSData *JSONData, NSError *error) {
        
        if (error != nil){
            completion(nil, error);
            return ;
        }
        
       NSDictionary *dic = [NSJSONSerialization  JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil];
        
        NSString *keyString = [dic.keyEnumerator nextObject];
        
        NSArray *dicArray = dic[keyString];
        
        NSMutableArray *modalsM = [NSMutableArray arrayWithCapacity:dicArray.count];
        
        for (NSDictionary *dic in dicArray) {
            
            [modalsM addObject:[CPNewModal newModalWithDictionary:dic]];
           
        }
    
        dispatch_async(dispatch_get_main_queue(), ^{
            completion(modalsM, error);
        });
    }];
    
    
    
}


+ (CPNewModal *)newModalWithDictionary:(NSDictionary *)dic{
    CPNewModal *modal = [[CPNewModal alloc] init];
    
    [modal setValue:dic[@"title"] forKey:@"title"];
    [modal setValue:dic[@"digest"] forKey:@"digest"];
    [modal setValue:dic[@"imgsrc"] forKey:@"imgsrc"];
    [modal setValue:dic[@"docid"] forKey:@"docid"];
    return  modal;
    
}


@end
